package com.kpx.app.wigdet;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kpx.app.bean.UserPurseInfo;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.kpx.app.R;
import com.kpx.app.utils.StringUtil;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.utils.UniversualImageLoaderUtils;
import com.kpx.app.wigdet.payment.GridPasswordView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 输入支付密码Dialog
 *
 * @author
 */
public class DialogInputPaymentPwd extends BaseDialog {
    private OnPositiveButtonClickListener onPositiveButtonClickListener;
    private OnNegativeButtonClickListener onNegativeButtonClickListener;
    private Context mContext;
    /**
     * 分润Container
     */
    @BindView(R.id.profitContainer)
    RelativeLayout profitContainer;
    /**
     * 可提现补贴余额Container
     */
    @BindView(R.id.returnContainer)
    RelativeLayout returnContainer;
    /**
     * 可消费补贴余额Container
     */
    @BindView(R.id.conRetContainer)
    RelativeLayout conRetContainer;
    /**
     * 现金退款Container
     */
    @BindView(R.id.refundContainer)
    RelativeLayout refundContainer;


    /**
     * 分润金额RadioButton
     */
    @BindView(R.id.profitRadioButton)
    RadioButton profitRadioButton;
    /**
     * 分润金额TextView
     */
    @BindView(R.id.profitMoneyTextView)
    TextView profitMoneyTextView;

    /**
     * 可提现补贴余额RadioButton
     */
    @BindView(R.id.returnRadioButton)
    RadioButton returnRadioButton;
    /**
     * 可提现补贴余额TextView
     */
    @BindView(R.id.returnMoneyTextView)
    TextView returnMoneyTextView;

    /**
     * 可消费补贴余额RadioButton
     */
    @BindView(R.id.conRetRadioButton)
    RadioButton conRetRadioButton;
    /**
     * 可消费补贴金额TextView
     */
    @BindView(R.id.conRetMoneyTextView)
    TextView conRetMoneyTextView;

    /**
     * 现金退款RadioButton
     */
    @BindView(R.id.refundRadioButton)
    RadioButton refundRadioButton;
    /**
     * 现金退款TextView
     */
    @BindView(R.id.refundMoneyTextView)
    TextView refundMoneyTextView;

    /**
     * 付款金额TextView
     */
    @BindView(R.id.payPriceTextView)
    TextView payPriceTextView;

    /**
     * 输入支付密码View
     */
    @BindView(R.id.password)
    GridPasswordView mPasswordView;
    /**
     * 确定按钮
     */
    @BindView(R.id.btnPositive)
    Button btnPositive;
    /**
     * 取消按钮
     */
    @BindView(R.id.btnNegative)
    Button btnNegative;

    /**
     * 消费分润提示Container
     */
    @BindView(R.id.profitAlertContainer)
    LinearLayout profitAlertContainer;

    /**
     * 顶部logo
     */
    @BindView(R.id.dialog_input_payment_pwd_image)
    ImageView inputPaymentPwdImage;

    /**
     * 用户输入的密码
     */
    private String passwordStr = "";

    /**
     * 用户钱包信息
     */
    private UserPurseInfo mUserPurseInfo;
    /**
     * 支付的订单金额
     */
    private double mPayPrice;
    /**
     * 分润余额
     */
    private final int PAY_MODE_PROFIT = 3;
    /**
     * 可提现补贴余额
     */
    private final int PAY_MODE_RETURN = 4;
    /**
     * 可消费补贴余额
     */
    private final int PAY_MODE_CON = 6;
    /**
     * 退款余额
     */
    private final int PAY_MODE_REFUND = 5;

    private int mSelectedPayMode = -1;

    /**
     * 当前店铺是否是直营店 1:是 0:不是
     */
    private int isDirectCamp;

    /**
     * 监听输入的密码
     */
    GridPasswordView.OnPasswordChangedListener passwordListener = new GridPasswordView.OnPasswordChangedListener() {

        // 密码
        @Override
        public void onMaxLength(String psw) {
            // 获取密码
            passwordStr = psw;
        }

        // 密码长度
        @Override
        public void onChanged(String psw) {
            if (psw.length() != 6) {
                btnPositive.setEnabled(false);
                btnPositive.setTextColor(Color.GRAY);
            } else {
                btnPositive.setEnabled(true);
                btnPositive.setTextColor(mContext.getResources().getColor(R.color.white));
            }
        }
    };

    public DialogInputPaymentPwd(Context context, OnPositiveButtonClickListener onPositiveButtonClickListener, OnNegativeButtonClickListener onNegativeButtonClickListener) {
        super(context);
        mContext = context;
        setContent();
        setOnPostitiveButtonClickListener(onPositiveButtonClickListener);
        setOnNegativeButtonClickListener(onNegativeButtonClickListener);
    }

    private void setContent() {
        setContentView(R.layout.dialog_input_payment_pwd_layout);
        ButterKnife.bind(this, this);

        returnContainer.setVisibility(View.GONE);
        conRetContainer.setVisibility(View.GONE);

        profitRadioButton.setChecked(true);
        profitRadioButton.setTextColor(mContext.getResources().getColor(R.color.red_text_color));
        profitMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.red_text_color));

        returnRadioButton.setChecked(false);
        returnRadioButton.setTextColor(mContext.getResources().getColor(R.color.common_black_color));
        returnMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.common_black_color));

        conRetRadioButton.setChecked(false);
        conRetRadioButton.setTextColor(mContext.getResources().getColor(R.color.common_black_color));
        conRetMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.common_black_color));
        conRetRadioButton.setText(Html.fromHtml("可消费补贴余额<br/>仅限胖胖直营店使用"));

        refundRadioButton.setChecked(false);
        refundRadioButton.setTextColor(mContext.getResources().getColor(R.color.common_black_color));
        refundMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.common_black_color));

        mPasswordView.setOnPasswordChangedListener(passwordListener);

        profitAlertContainer.setVisibility(View.GONE);

        ImageLoader.getInstance().displayImage(UniversualImageLoaderUtils.generateLocalResImgUri(R.drawable.ic_launcher), inputPaymentPwdImage, UniversualImageLoaderUtils.getDisplayImageOptions());
    }

    /**
     * 确定
     */
    public void setOnPostitiveButtonClickListener(OnPositiveButtonClickListener listener) {
        this.onPositiveButtonClickListener = listener;
    }

    /**
     * 取消
     */
    public void setOnNegativeButtonClickListener(OnNegativeButtonClickListener listener) {
        this.onNegativeButtonClickListener = listener;
    }

    @OnClick({R.id.btnPositive, R.id.btnNegative, R.id.profitContainer, R.id.returnContainer, R.id.conRetContainer, R.id.refundContainer, R.id.profitRadioButton, R.id.refundRadioButton, R.id.conRetRadioButton, R.id.returnRadioButton})
    public void onClickByButterknife(View v) {
        switch (v.getId()) {
            case R.id.profitRadioButton: //分润余额
            case R.id.profitContainer:
                if (null == mUserPurseInfo) return;
                if (mUserPurseInfo.getRunSubTotalAmount() < mPayPrice) {
                    ToastUtil.getInstance().showToast(mContext, "分润余额不足");
                    profitRadioButton.setChecked(false);
                    return;
                }
                mSelectedPayMode = PAY_MODE_PROFIT;
                profitRadioButton.setChecked(true);
                profitRadioButton.setTextColor(mContext.getResources().getColor(R.color.red_text_color));
                profitMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.red_text_color));

                returnRadioButton.setChecked(false);
                returnRadioButton.setTextColor(mContext.getResources().getColor(R.color.common_black_color));
                returnMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.common_black_color));

                conRetRadioButton.setChecked(false);
                conRetRadioButton.setTextColor(mContext.getResources().getColor(R.color.common_black_color));
                conRetMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.common_black_color));

                refundRadioButton.setChecked(false);
                refundRadioButton.setTextColor(mContext.getResources().getColor(R.color.common_black_color));
                refundMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.common_black_color));

                profitAlertContainer.setVisibility(View.VISIBLE);
                break;
            case R.id.returnRadioButton: //可提现补贴余额
            case R.id.returnContainer:
                if (null == mUserPurseInfo) return;
                if (mUserPurseInfo.getCashBackTotalAmount() < mPayPrice) {
                    ToastUtil.getInstance().showToast(mContext, "可提现补贴余额不足");
                    returnRadioButton.setChecked(false);
                    return;
                }
                mSelectedPayMode = PAY_MODE_RETURN;
                profitRadioButton.setChecked(false);
                profitRadioButton.setTextColor(mContext.getResources().getColor(R.color.common_black_color));
                profitMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.common_black_color));

                returnRadioButton.setChecked(true);
                returnRadioButton.setTextColor(mContext.getResources().getColor(R.color.red_text_color));
                returnMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.red_text_color));

                conRetRadioButton.setChecked(false);
                conRetRadioButton.setTextColor(mContext.getResources().getColor(R.color.common_black_color));
                conRetMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.common_black_color));

                refundRadioButton.setChecked(false);
                refundRadioButton.setTextColor(mContext.getResources().getColor(R.color.common_black_color));
                refundMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.common_black_color));

                profitAlertContainer.setVisibility(View.VISIBLE);
                break;
            case R.id.conRetContainer: //可消费补贴余额
            case R.id.conRetRadioButton:
                if (null == mUserPurseInfo) return;
                if (isDirectCamp == 0) {
                    ToastUtil.getInstance().showToast(mContext, "可消费补贴余额仅限胖胖直营店使用");
                    conRetRadioButton.setChecked(false);
                    return;
                }
                if (mUserPurseInfo.getConsumeTotalAmount() < mPayPrice) {
                    ToastUtil.getInstance().showToast(mContext, "可消费补贴余额不足");
                    conRetRadioButton.setChecked(false);
                    return;
                }
                mSelectedPayMode = PAY_MODE_CON;
                profitRadioButton.setChecked(false);
                profitRadioButton.setTextColor(mContext.getResources().getColor(R.color.common_black_color));
                profitMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.common_black_color));

                returnRadioButton.setChecked(false);
                returnRadioButton.setTextColor(mContext.getResources().getColor(R.color.common_black_color));
                returnMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.common_black_color));

                conRetRadioButton.setChecked(true);
                conRetRadioButton.setTextColor(mContext.getResources().getColor(R.color.red_text_color));
                conRetMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.red_text_color));

                refundRadioButton.setChecked(false);
                refundRadioButton.setTextColor(mContext.getResources().getColor(R.color.common_black_color));
                refundMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.common_black_color));

                profitAlertContainer.setVisibility(View.VISIBLE);
                break;
            case R.id.refundRadioButton: //现金退款
            case R.id.refundContainer:
                if (null == mUserPurseInfo) return;
                if (mUserPurseInfo.getReimburseTotalAmount() < mPayPrice) {
                    ToastUtil.getInstance().showToast(mContext, "现金退款余额不足");
                    refundRadioButton.setChecked(false);
                    return;
                }
                mSelectedPayMode = PAY_MODE_REFUND;
                profitRadioButton.setChecked(false);
                profitRadioButton.setTextColor(mContext.getResources().getColor(R.color.common_black_color));
                profitMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.common_black_color));

                returnRadioButton.setChecked(false);
                returnRadioButton.setTextColor(mContext.getResources().getColor(R.color.common_black_color));
                returnMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.common_black_color));

                conRetRadioButton.setChecked(false);
                conRetRadioButton.setTextColor(mContext.getResources().getColor(R.color.common_black_color));
                conRetMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.common_black_color));

                refundRadioButton.setChecked(true);
                refundRadioButton.setTextColor(mContext.getResources().getColor(R.color.red_text_color));
                refundMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.red_text_color));
                profitAlertContainer.setVisibility(View.GONE);
                break;
            case R.id.btnPositive:
                if (mSelectedPayMode == -1) {
                    ToastUtil.getInstance().showToast(mContext, "请选择支付方式!");
                    return;
                }
                if (onPositiveButtonClickListener != null) {
                    onPositiveButtonClickListener.onPositiveButtonClick(passwordStr, mSelectedPayMode);
                }
                break;
            case R.id.btnNegative:
                if (onNegativeButtonClickListener != null)
                    onNegativeButtonClickListener.oNegativeButtonClick();
                break;
        }
    }

    /**
     * 确定监听器
     */
    public interface OnPositiveButtonClickListener {
        void onPositiveButtonClick(String payPwd, int payMode); // 点击确定按钮
    }

    /**
     * 取消监听器
     */
    public interface OnNegativeButtonClickListener {
        void oNegativeButtonClick(); // 点击取消按钮
    }

    @Override
    public void show() {
        super.show();
        if (null != mPasswordView) {
            mPasswordView.clearPassword();
        }
        // 判断密码长度是否满足6位，如果不满足 确定按钮不能点击，文字颜色变灰色
        if (passwordStr.length() != 6) {
            btnPositive.setEnabled(false);
            btnPositive.setTextColor(Color.GRAY);
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        passwordStr = "";
        mUserPurseInfo = null;
        mPayPrice = 0;
        mSelectedPayMode = -1;
    }

    public void setDataToDialog(UserPurseInfo userPurseInfo, double payPrice, int isDirectCamp) {
        if (null == userPurseInfo || payPrice < 0) return;
        mUserPurseInfo = userPurseInfo;
        mPayPrice = payPrice;
        this.isDirectCamp = isDirectCamp;
        profitMoneyTextView.setText(Html.fromHtml(toHtml(userPurseInfo.getRunSubTotalAmount())));
        returnMoneyTextView.setText(Html.fromHtml(toHtml(userPurseInfo.getCashBackTotalAmount())));
        conRetMoneyTextView.setText(Html.fromHtml(toHtml(userPurseInfo.getConsumeTotalAmount())));
        refundMoneyTextView.setText(Html.fromHtml(toHtml(userPurseInfo.getReimburseTotalAmount())));
        payPriceTextView.setText(Html.fromHtml(toHtml(payPrice)));

        profitRadioButton.setTextColor(mContext.getResources().getColor(R.color.common_black_color));
        profitRadioButton.setChecked(false);
        profitMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.common_black_color));

        returnRadioButton.setTextColor(mContext.getResources().getColor(R.color.common_black_color));
        returnRadioButton.setChecked(false);
        returnMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.common_black_color));

        conRetRadioButton.setTextColor(mContext.getResources().getColor(R.color.common_black_color));
        conRetRadioButton.setChecked(false);
        conRetMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.common_black_color));
        if (isDirectCamp == 0) {
            conRetRadioButton.setTextColor(mContext.getResources().getColor(R.color.lightTextColor));
        }

        refundRadioButton.setTextColor(mContext.getResources().getColor(R.color.common_black_color));
        refundRadioButton.setChecked(false);
        refundMoneyTextView.setTextColor(mContext.getResources().getColor(R.color.common_black_color));

        if (userPurseInfo.getRunSubTotalAmount() < payPrice
                && userPurseInfo.getCashBackTotalAmount() < payPrice
                && userPurseInfo.getReimburseTotalAmount() < payPrice
                && userPurseInfo.getConsumeTotalAmount() < payPrice) {
            mPasswordView.setEnabled(false);
        }
    }

    private String toHtml(double money) {
        String moneyStr = StringUtil.formatAmount(money, 2);
        String[] arr = moneyStr.split("[.]");
        if (arr.length > 1) {
            return "¥" + arr[0] + "" + "<small>." + arr[1] + "</small>";
        } else {
            return "¥" + money + "" + "<small>.00</small>";
        }
    }

}
